package com.ktjy.controller;

import com.ktjy.entity.SysUser;
import com.ktjy.service.SysUserService;
import com.ktjy.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController extends BaseController {

    @Autowired
    private SysUserService sysUserService;
    /**
     * 跳转到登录页面
     * @return
     */
    @GetMapping(value = "/login")
    public String toLogin(){
        return forward("/login.jsp");
    }
    /**
     * 登录接口
     * @param account
     * @param password
     * @return
     */
    @PostMapping(value = "/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password) {
        SysUser loginUser = sysUserService.login(account,password);
        if(loginUser!=null){//登录成功
            session(Constants.SESSION_USER,loginUser);
            return redirect("/main/index");
        }else{
            request(Constants.REQUEST_ERROR, "用户名或密码错误！ ");
            return forward("/login.jsp");
        }
    }
    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public String logout(){
        session().invalidate();
        return redirect("/login");
    }
    // 跳转到后台首页
    @GetMapping("/main/index")
    public String index(){
        return "index";
    }
}